<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理员登录</title>
    <link rel="stylesheet" href="static/css/login.css">
    <script type="text/javascript" src="static/js/jquery.min.js"></script>
    <script src="static/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body class="login-bg">

<div class="login">
    <div class="message">管理员后台登录</div>
    <div id="darkbannerwrap"></div>
    <form id="loginForm" class="layui-form">
        <input name="username" placeholder="用户名" type="text" lay-verify="required" class="layui-input">
        <hr class="hr15">
        <input name="password" lay-verify="required" placeholder="密码" type="password" class="layui-input">
        <hr class="hr15">
        <button type="submit"
                style="width: 100%;height: 50px; padding: 10px; background-color: #2fb9d4; color: #fff; border: none; border-radius: 4px; cursor: pointer;">
            登录
        </button>
        <hr class="hr20">
    </form>
</div>

<script>
    const loginForm = document.getElementById('loginForm');

    loginForm.addEventListener('submit', (e) => {
        e.preventDefault();

        const formData = new FormData(loginForm);
        const xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function () {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.status === 200) {
                    const response = xhr.responseText;

                    if (response === 'error') {
                        alert('登录失败：账号或密码错误');
                    } else if (response == 'not admin') {
                        alert('你不是管理员')
                    } else {
                        // 登录成功时进行重定向
                        window.location.href = '/index';
                    }
                } else {
                    // 处理请求错误情况
                    alert('请求错误：' + xhr.status);
                }
            }
        };

        xhr.open('POST', '/adminDoLogin');
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send(new URLSearchParams(formData).toString());
    });
</script>
</body>
</html>